package com.sumer.psychology.sharing.domain.model;

import com.sumer.psychology.common.Assert;
import com.sumer.psychology.common.domain.model.IdentifiedValueObject;

import java.util.Date;

/**
 * 分享文件
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/9
 * Time: 15:18
 */
public class SharedFile extends IdentifiedValueObject {
    private String name;
    private String fileId;
    private Date createTime;
    private SharedFileType fileType;

    /**
     * 创建文件
     *
     * @param name   file name
     * @param fileId file id
     */
    public SharedFile(String name, String fileId) {
        setName(name);
        setFileId(fileId);
        setCreateTime(new Date());
        setFileType(fileType());
    }

    public SharedFile() {
    }

    private void setName(String name) {
        Assert.notEmpty(name, "分享文件名称不能为空");
        Assert.length(name, 100, "文件名不能超过50个字");

        this.name = name;
    }

    private void setFileId(String fileId) {
        Assert.notEmpty(fileId, "文件Id不能为空");

        this.fileId = fileId;
    }

    private void setCreateTime(Date createTime) {
        Assert.notNull(createTime, "创建时间不能为空");

        this.createTime = createTime;
    }

    private void setFileType(SharedFileType fileType) {
        Assert.notNull(fileType, "文件类型不能为空");

        this.fileType = fileType;
    }

    public String name() {
        return name;
    }

    public String fileId() {
        return fileId;
    }

    public Date createTime() {
        return createTime;
    }

    /**
     * 文件类型
     */
    public SharedFileType fileType() {
        if (this.fileType != null) {
            return this.fileType;
        }

        Assert.notEmpty(name, "分享文件名称不能为空");

        if (name().contains(".")) {
            String suffix = name().substring(name().lastIndexOf(".") + 1);
            SharedFileType fileType = SharedFileType.get(suffix.toLowerCase());
            if (fileType != null) {
                return fileType;
            }
        }
        return SharedFileType.UNKNOWN;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SharedFile that = (SharedFile) o;

        if (fileId != null ? !fileId.equals(that.fileId) : that.fileId != null) return false;
        if (fileType != that.fileType) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (fileId != null ? fileId.hashCode() : 0);
        result = 31 * result + (fileType != null ? fileType.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "SharedFile{" +
                "name='" + name + '\'' +
                ", fileId='" + fileId + '\'' +
                ", createTime=" + createTime +
                ", fileType=" + fileType +
                '}';
    }
}
